package com.coder.oj.admin.controller;

import com.coder.oj.admin.dto.RoleDto;
import com.coder.oj.admin.dto.RoleOauthDto;
import com.coder.oj.admin.mapstruct.RoleMapstruct;
import com.coder.oj.admin.service.RoleService;
import com.coder.oj.admin.vo.RoleVo;
import com.coder.oj.common.core.JwtToken;
import com.coder.oj.common.core.R;
import com.coder.oj.common.mapper.RoleMapper;
import com.coder.oj.common.validate.ValidationGroups;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = {"角色管理"})
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMapstruct roleMapstruct;

    @JwtToken
    @GetMapping("/list")
    @ApiOperation("角色列表")
    public R<List<RoleVo>> getRoleList() {
        return R.ok().setResult(roleService.getRoleList());
    }

    @JwtToken
    @GetMapping
    @ApiOperation("角色详情")
    public R<RoleVo> getRoleById(@RequestParam("id") Long id) {
        return R.ok().setResult(roleMapstruct.toVO(roleMapper.selectById(id)));
    }

    @JwtToken
    @PostMapping
    @ApiOperation("新增角色")
    public R saveRole(@Validated(ValidationGroups.Insert.class) @RequestBody RoleDto params) {
        roleMapper.insert(roleMapstruct.toEntity(params));
        return R.ok();
    }

    @JwtToken
    @PutMapping
    @ApiOperation("修改角色")
    public R updateRole(@Validated(ValidationGroups.Update.class) @RequestBody RoleDto params) {
        roleService.updateRole(params);
        return R.ok();
    }

    @JwtToken
    @DeleteMapping
    @ApiOperation("删除角色")
    public R deleteRoleById(@RequestParam("id") Long id) {
        roleService.deleteRoleById(id);
        return R.ok();
    }

    @JwtToken
    @GetMapping("/admin/ids")
    @ApiOperation("管理员角色id集合")
    public R<List<String>> getRoleIdsByAdminId(@RequestParam("id") Long id) {
        return R.ok().setResult(roleService.getRoleIdsByAdminId(id));
    }
}
